package strutsNcrank.example;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.crank.crud.GenericDao;
import org.crank.crud.controller.FilteringPaginator;
import org.crank.crud.model.Department;
import org.crank.crud.model.Employee;

import com.opensymphony.xwork2.ActionSupport;

public class EmployeeAction extends ActionSupport {
	Log log = LogFactory.getLog(Employee.class);
	GenericDao<Employee, Long> employeeDao;
	GenericDao<Department, Long> departmentDao;
	FilteringPaginator pager;	//TODO how to get a DaoFilteringPagingDataSource?
	List<Employee> employees;
	
	static int nameIdx = 0;
	
	public String execute() {
		employees = employeeDao.find();
		return SUCCESS;
	}
	
	public String createEmployee() {
		Employee e = new Employee();
		e.setFirstName("Foo" + nameIdx++);
		e.setLastName("Bar");
		e.setNumberOfPromotions(0);
		Department dept = new Department();
		dept.setName("Sales");
		dept.addEmployee(e);
		departmentDao.persist(dept);
		
		addActionMessage("New employee added: " + e.getFirstName());
		return SUCCESS;
	}
	
	public int getEmployeeCount() {
		log.debug("get Employee count");
		return employeeDao.count();
	}

	public GenericDao<Employee, Long> getEmployeeDao() {
		return employeeDao;
	}

	public void setEmployeeDao(GenericDao<Employee, Long> employeeDao) {
		this.employeeDao = employeeDao;
	}

	public GenericDao<Department, Long> getDepartmentDao() {
		return departmentDao;
	}

	public void setDepartmentDao(GenericDao<Department, Long> departmentDao) {
		this.departmentDao = departmentDao;
	}

	public List<Employee> getEmployees() {
		return employees;
	}

	public void setEmployees(List<Employee> employees) {
		this.employees = employees;
	}
}
